package com.financing.wap.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.financing.wap.common.SqlName;
import com.financing.wap.dao.IAccountDao;
import com.financing.wap.dto.AccountCorpDTO;
import com.financing.wap.dto.AccountDetailDTO;
import com.financing.wap.vo.AccountCorpVo;
import com.financing.wap.vo.AccountDetailVo;
import com.financing.wap.vo.AmountVO;



@Repository("accountDao")
@SuppressWarnings(value={"unchecked", "deprecation"})
public class AccountDaoImpl extends BaseDaoImpl<AccountCorpDTO, java.lang.Integer> implements IAccountDao {

	@Override
	public Class<AccountCorpDTO> getEntityClass() {
		return AccountCorpDTO.class;
	}

	@Override
	public AccountCorpDTO getAccountCorpInfo(AccountCorpVo vo) {
		AccountCorpDTO result = (AccountCorpDTO)this.getSqlMapClientTemplate().queryForObject(SqlName.ACCOUNT_CORP_INFO,vo);
		return result;
	}

	@Override
	public List<AccountCorpDTO> getAccountDetailCorpList(
			AccountCorpVo vo) {
		List<AccountCorpDTO> result = this.getSqlMapClientTemplate().queryForList(SqlName.ACCOUNT_CORP_DETAIL_LIST,vo);
		return result;
	}

	@Override
	public int getAccountDetailCorpCount(AccountCorpVo vo) {
		Integer count = (Integer)this.getSqlMapClientTemplate().queryForObject(SqlName.ACCOUNT_CORP_DETAIL_COUNT,vo);
		return count;
	}

	@Override
	public AccountCorpDTO getFaAccountByUserId(String id) {
		AccountCorpDTO result = (AccountCorpDTO)this.getSqlMapClientTemplate().queryForObject(SqlName.ACCOUNT_USER,id);
		return result;
	}

	@Override
	public Integer updateAccountBalanceActivity(AccountCorpDTO dto) {
		this.sqlMapClientTemplate.update(SqlName.UPDATE_ACCOUNT_BALANCE_ACTIVITY,dto);
		return dto.getId();
	}

	@Override
	public List<Map<String,Object>> getCorpUserAmount(AmountVO queryVo) {
		 List<Map<String,Object>> resultList=this.getSqlMapClientTemplate().queryForList(SqlName.AMOUNT_USER,queryVo);
		return resultList;
	}
	
	@Override
	public Integer getCorpUserAmountCount(AmountVO queryVo) {
		Integer count =  (Integer)this.getSqlMapClientTemplate().queryForObject(SqlName.AMOUNT_USER_COUNT,queryVo);
		return count;
	}

	@Override
	public Map<String,Object> getCorpAllAmount(String corpId) {
		Map<String,Object> resultMap= (Map<String,Object>)this.getSqlMapClientTemplate().queryForObject(SqlName.AMOUNT_ALL,corpId);
		return resultMap;
	}

	@Override
	public List<Map<String,Object>> getCorpDeptAmount(String corpId) {
		List<Map<String,Object>> resultList= this.getSqlMapClientTemplate().queryForList(SqlName.AMOUNT_DEPT,corpId);
		return resultList;
	}

	@Override
	public Integer getUserAccountDetailCount(AccountDetailVo queryVo) {
		Integer count =  (Integer)this.getSqlMapClientTemplate().queryForObject(SqlName.USER_ACCOUNT_DETAIL_COUNT,queryVo);
		return count;
	}

	@Override
	public List<AccountDetailDTO> getUserAccountDetailList(
			AccountDetailVo queryVo) {
		List<AccountDetailDTO> resultList = this.getSqlMapClientTemplate().queryForList(SqlName.USER_ACCOUNT_DETAIL_LIST,queryVo);
		return resultList;
	}

	@Override
	public Boolean addAccount(AccountCorpDTO account) {
		if(StringUtils.isBlank(account.getUserId())||"-1".equals(account.getUserId())){
			return false;
		}
		try{
			this.getSqlMapClientTemplate().insert(SqlName.USER_ACCOUNT_ADD,account);
			return true;
		}catch(Exception e){
			log.error("add user account error.",e);
			return false;
		}
	}

	@Override
	public Integer addUserAccountDetail(AccountDetailDTO dto) {
		Integer id =  (Integer)this.getSqlMapClientTemplate().insert(SqlName.USER_ACCOUNT_DETAIL_ADD,dto);
		return id;
	}
}
